﻿namespace Smart.Utils.UI.RoundButton.Utils.Text
{
    using System;
    using System.ComponentModel;
    using System.Drawing;

    public class VerticalString
    {
        private double textSpread = 0.75;

        [Description("Draw Method 1 - draw string in top left of rectangle.Calls Method 3")]
        public void Draw(Graphics g, string text, Font font, Brush brush, Rectangle stringRect)
        {
            this.Draw(g, text, font, brush, stringRect.X, stringRect.Y);
        }

        [Description("Draw Method 2 - draw string in rectangle according to Alignment and LineAlignmentCalls Method 3")]
        public void Draw(Graphics g, string text, Font font, Brush brush, Rectangle stringRect, StringFormat stringStrFmt)
        {
            int num;
            int num2;
            switch (stringStrFmt.Alignment)
            {
                case StringAlignment.Center:
                    num = ((stringRect.Width / 2) - ((int) (font.Size / 2f))) - 2;
                    break;

                case StringAlignment.Far:
                    num = (stringRect.Width - ((int) font.Size)) - 2;
                    break;

                default:
                    num = 0;
                    break;
            }
            double num3 = this.Length(text, font);
            switch (stringStrFmt.LineAlignment)
            {
                case StringAlignment.Center:
                    num2 = (stringRect.Height / 2) - ((int) (num3 / 2.0));
                    break;

                case StringAlignment.Far:
                    num2 = (stringRect.Height - ((int) num3)) - 2;
                    break;

                default:
                    num2 = 0;
                    break;
            }
            this.Draw(g, text, font, brush, (int) (stringRect.X + num), (int) (stringRect.Y + num2));
        }

        [Description("Draw Method 3 - draw string at coordinates x, y")]
        public void Draw(Graphics g, string text, Font font, Brush brush, int x, int y)
        {
            char[] chArray = text.ToCharArray();
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            Rectangle layoutRectangle = new Rectangle(x, y, (int) (font.Size * 1.5), font.Height);
            for (int i = 0; i < text.Length; i++)
            {
                layoutRectangle.Offset(0, this.ExtraSpaceAllowance(esaType.Pre, chArray[i], font));
                g.DrawString(chArray[i].ToString(), font, brush, layoutRectangle, format);
                layoutRectangle.Offset(0, (int) (font.Height * this.textSpread));
                layoutRectangle.Offset(0, this.ExtraSpaceAllowance(esaType.Post, chArray[i], font));
            }
        }

        private int ExtraSpaceAllowance(esaType type, char ch, Font font)
        {
            if (this.textSpread >= 1.0)
            {
                return 0;
            }
            int num = 0;
            if (((type == esaType.Pre) | (type == esaType.Either)) && (" bdfhijkltABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".IndexOf(ch) > 0))
            {
                num += (int) (font.Height * 0.2);
            }
            if (((type == esaType.Post) | (type == esaType.Either)) && (" gjpqyQ".IndexOf(ch) > 0))
            {
                num += (int) (font.Height * 0.2);
            }
            return num;
        }

        [Description("Length Method - returns vertical length of string")]
        public int Length(string text, Font font)
        {
            char[] chArray = text.ToCharArray();
            int num = 0;
            for (int i = 0; i < text.Length; i++)
            {
                num += (int) (font.Height * this.textSpread);
                num += this.ExtraSpaceAllowance(esaType.Either, chArray[i], font);
            }
            return num;
        }

        public double TextSpread
        {
            get
            {
                return this.textSpread;
            }
            set
            {
                this.textSpread = value;
            }
        }

        private enum esaType
        {
            Pre,
            Post,
            Either
        }
    }
}

